package com.sojpt.boot.controller.admin;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.IdKit;
import com.sojpt.model.TybkCorporate;
import com.sojpt.model.TybkQualifications;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Sohnny
 */
@RestController
@RequestMapping("/admin/corporate")
public class TybkCorporateAdminController extends IViewController {

    @RequestMapping("/list")
    public void list() {

        TybkCorporate tybkCorporate = getModel(TybkCorporate.class, "tybkCorporate");
        tybkCorporate.put("create_time_start_time", getPara("create_time_start_time"));
        tybkCorporate.put("create_time_end_time", getPara("create_time_end_time"));
        tybkCorporate.put("modification_time_start_time", getPara("modification_time_start_time"));
        tybkCorporate.put("modification_time_end_time", getPara("modification_time_end_time"));
        tybkCorporate.put("deletion_time_start_time", getPara("deletion_time_start_time"));
        tybkCorporate.put("deletion_time_end_time", getPara("deletion_time_end_time"));

        SqlPara sqlPara = Db.getSqlPara("admin_corporate.select", getKv(tybkCorporate));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @RequestMapping("/save")
    public void save() {
        TybkCorporate tybkCorporate = getModel(TybkCorporate.class, "tybkCorporate");
        tybkCorporate.setId(IdKit.generatorID());
        tybkCorporate.setCreateTime(new Date());
        tybkCorporate.setDeleted(0);
        if (tybkCorporate.save()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @RequestMapping("/update")
    public void update() {
        TybkCorporate tybkCorporate = getModel(TybkCorporate.class, "tybkCorporate");
        tybkCorporate.setModificationTime(new Date());
        if (tybkCorporate.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @RequestMapping("/delete")
    public void delete() {
        TybkCorporate tybkCorporate = getModel(TybkCorporate.class, "tybkCorporate");
        tybkCorporate.setDeletionTime(new Date());
        tybkCorporate.setDeleted(1);
        if (tybkCorporate.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @RequestMapping("/batchDelete")
    public void batchDelete() {
        int a = Db.update("UPDATE tybk_corporate SET deleted=1,deletion_time=NOW() WHERE id in" + "(" + getPara("ids") + ")");
        if (a > 0) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    /**
     * 查询所有的资质
     */
    @RequestMapping("/getAllQualifications")
    public void getAllQualifications() {
        List<TybkQualifications> tybkQualifications = TybkQualifications.dao.find("SELECT id,qualifications_name FROM tybk_qualifications WHERE deleted = 0");
        renderJson(tybkQualifications);
    }


}